package cn.com.pansky.action.bank;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.vsame.url2sql.sql.Model;
import cn.com.pansky.getui.AppPush;
import cn.com.pansky.service.NoticeService;
import cn.com.pansky.service.OrderBackWriteService;

import com.abc.pay.client.TrxException;
import com.abc.pay.client.ebus.PaymentResult;
import com.alibaba.fastjson.JSONObject;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.template.TransmissionTemplate;

/**
 * 农行支付通知
 */
public class ABCPayResult extends HttpServlet {
	
	private static final long serialVersionUID = 8993095832248461186L;
	
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		NoticeService notice = new NoticeService();
		OrderBackWriteService orderBackWriteService = new OrderBackWriteService(); // 银行支付成功，系统数据回填接口
		// 取得MSG参数，并利用此参数值生成支付结果对象
		String msg = req.getParameter("MSG");
		Map<String, String> map = new HashMap<String, String>();
		String code = "";
		try {
			PaymentResult tResult = new PaymentResult(msg);
			if(tResult.isSuccess()) {
				map.put("bankType", "NH"); // 银行类别
				map.put("AcqSsn", tResult.getValue("iRspRef")); // 银行缴费流水号
				map.put("TranAmt", tResult.getValue("Amount")); // 缴费金额
				map.put("TermSsn", tResult.getValue("OrderNo")); // 订单号
				// 支付成功
				// 支付成功推送消息
				TransmissionTemplate template = new TransmissionTemplate();
				APNPayload payload = new APNPayload();
				AppPush push = new AppPush();
				Model user = (Model) req.getSession().getAttribute("user");
				String alias = user.getString("AA001"); // 用户ID
				map.put("AAE011", alias);
//				String alias = "602c8a5b-5748-47cd-b4d3-6f9fa0425cdd";
				// 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
				JSONObject pushData = new JSONObject();
				pushData.put("t", "1");//业务办理类型
				pushData.put("m", "缴费通知");//通知头
				pushData.put("d", "缴费成功");//通知内容
				if(AppPush.getUserStatus(alias)) {
					template.setTransmissionContent(pushData.toString());
				}else{
					payload.addCustomMsg("t", pushData.get("t"));
					payload.addCustomMsg("m", pushData.get("m"));
					payload.addCustomMsg("d", pushData.get("d"));
					template.setAPNInfo(payload);
				}
				try {
					push.pushtoSingle(template, alias, payload);
				} catch (Exception e) {
					e.printStackTrace();
				}
				notice.add(pushData, alias); // 推送消息
				map.put("AAC002", req.getSession().getAttribute("orderWriteCA006").toString()); // 身份证号
				map.put("AAE010", "999999");
				JSONObject object = orderBackWriteService.orderBackWrite(map); // 回填订单信息
				if(object.getBooleanValue("SUCCESS")) {
					code = "ok";
				} else {
					code = "301";
				}
			} else {
				// 支付失败
				code = "301";
			}
			resp.sendRedirect("bank/bankResult.html?code="+code);
		} catch (TrxException e) {
			e.printStackTrace();
		}
	}
}
